<%# Params:
#     - page  (Page Node)
#     - level (integer indicating current tree depth)
#   Called From:
#     - index
#   Description:
#     This partial is used recursively. Render it with the root node, and it will recurse
#     down all of the child nodes to build a list with proper indentation to indicate
#     tree depth.
%>

<%
# Create the page-level links...
links = []
links << link_to(pluralize(page.versions.length, 'revision', 'revisions'), :action=>'versions', :id=>page) if page.versions.length > 0
links << link_to('add child page', {:action=>'new', :parent=>page}, :title=>"Add a child to '#{page.title}'", :class=>'add-page')
links << link_to_function('reorder children', "ComatoseList.toggle_reorder('page_list_#{page.id}',this,#{page.id})", :title=>"Reorder children of '#{page.title}'", :class=>'reorder-children', :href=>url_for(:action=>'reorder', :id=>page)) if !page.children.empty? and page.children.length > 1
links << link_to('delete', {:action=>'delete', :id=>page}, :confirm=>'This will delete this page, and any children. Are you sure?', :title=>"Delete page '#{page.title}' and all it's children", :class=>'delete-page', :method=>'post', :onmouseover=>"ComatoseList.item_hover('page_#{page.id}', 'over', true)", :onmouseout=>"ComatoseList.item_hover('page_#{page.id}', 'out', true)") unless @root_pages.include? page
# Level check, one, two, three...
collapse_children = (level >= Comatose.config.default_tree_level)
%>

<li id="page_<%= page.id %>">
  <table cellpadding="0" cellspacing="0">
    <tr>
      <td rowspan="2" valign="center">
      <% if !page.children.empty? %>
        <%=  image_tag( ((collapse_children) ? 'comatose/collapsed.gif' : 'comatose/expanded.gif'), :title=>'Expand/Collapse', :onclick=>"ComatoseList.toggle_tree_nodes(this,#{page.id});", :class=>'tree-controller', :size=>'12x12', :id=>"page_controller_#{page.id}" ) %>
      <% else %>
        <%= image_tag 'comatose/no-children.gif', :size=>'12x12', :class=>'tree-controller' %>
      <% end %>        
      </td>
      <td rowspan="2" valign="center">
        <%= image_tag 'comatose/page.gif', :size=>'25x31', :align=>"absmiddle" %>
        <span class="handle">DRAG</span>
      </td>
      <td>
        <%= link_to page.title, {:action=>'edit', :id=>page}, :title=>"Path:#{page.full_path}", :class=>'page' %>
      </td>
    </tr>
    <tr>
      <td class="commands">
        Updated <span title="Created on <%= page.created_on %>"><%= time_ago_in_words page.updated_on, true %> ago</span><%= " by #{page.author}" unless page.author.nil? or page.author.empty? %>,
        <%= links.join(', ') %>.
      </td>
    </tr>
  </table>

  <ul id="page_list_<%= page.id %>" old="lvl-<%= page.id %>" class="page-list <%= 'collapsed' if collapse_children %>" >
    <% for child in page.children %>
      <%= render :partial=>'page_list_item', :locals=>{ :page=>child, :level=>level+1 } %>
    <% end %>
  </ul>

  <%= sortable_element( "page_list_#{page.id}", 
        :complete => visual_effect(:highlight, "page_list_#{page.id}"), 
        :handle=>'handle',
        :update=>'flash-content',
        :url => { :action => "reorder", :id=>page } ) if !page.children.empty? and page.children.length > 1 %>
</li>
